package com.vpclub.test;

import com.vpclub.bean.Car;
import com.vpclub.bean.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Map;
import java.util.Properties;

/**
 * @author : leimingchao
 * @version V1.0
 * @Project: springdemo
 * @Package com.vpclub.test
 * @Description: TODO
 * @date Date : 2021年05月13日 22:46
 */
public class IOCTest2 {

    private ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");


    /**
     * 实验4：正确的为各种属性赋值
     * 测试使用null值，默认引用类型就是null，基本类型是默认值
     */
    @Test
    public void test() {
        Person bean = (Person) applicationContext.getBean("person01");
        System.out.println(bean.getLastName() == null);
    }

    @Test
    public void test02() {
        Person person = (Person) applicationContext.getBean("person01");
        System.out.println(person);
        System.out.println("person的car：" + person.getCar());
        System.out.println("===========================");
        Car bean = (Car) applicationContext.getBean("car01");
        bean.setCarName("haha");
        System.out.println("我修改了容器中的car，你的car变了没？" + person.getCar());
        Car car = person.getCar();
        System.out.println(bean == car);
    }

    @Test
    public void test03() {
        Person person = (Person) applicationContext.getBean("person01");
        Car car = person.getCar();
        System.out.println(car);
    }

    @Test
    public void test04() {
        Person person = (Person) applicationContext.getBean("person02");
        person.getBooks().stream().forEach(System.out::println);

        System.out.println("===============================");
        /**
         * 内部bean是不能用id获取的
         * org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'carInner' is defined
         */
        Object bean = applicationContext.getBean("carInner");
        System.out.println(bean);
    }

    @Test
    public void test05() {
        Person person = (Person) applicationContext.getBean("person02");
        Map<String, Object> maps = person.getMaps();
        System.out.println(maps);
    }

    @Test
    public void test06() {
        Person person = (Person) applicationContext.getBean("person02");
        Properties properties = person.getProperties();
        System.out.println(properties);
    }

    @Test
    public void test07() {
        Person person = (Person) applicationContext.getBean("person03");
        Map<String, Object> maps = person.getMaps();
        System.out.println(maps);
        Object bean = applicationContext.getBean("myMap");
        System.out.println(bean.getClass());
    }

    /**
     * 级联属性可以修改属性的属性，注意：原来的bean的值可能会被修改
     */
    @Test
    public void test08() {
        Person person = (Person) applicationContext.getBean("person04");
        Object car = applicationContext.getBean("car01");
        System.out.println("容器中的car：" + car);
        System.out.println("person中的car：" + person.getCar());
    }

    @Test
    public void test09() {
        Person person= (Person) applicationContext.getBean("person06");
        System.out.println("person06:" + person);
    }

    /**
     * org.springframework.beans.factory.BeanIsAbstractException:
     * Error creating bean with name 'person05': Bean definition is abstract
     */
    @Test
    public void test10() {
        Person person= (Person) applicationContext.getBean("person05");
        System.out.println("person05:" + person);
    }

    @Test
    public void test11() {
        Object myList = applicationContext.getBean("myList");
        System.out.println("myList:" + myList);
    }
}
